package com.qhg.saas_server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhg.saas_server.mapper.VisitLogMapper;
import com.qhg.saas_server.pojo.dto.VisitLogDto;
import com.qhg.saas_server.pojo.dto.VisitStatisticsDto;
import com.qhg.saas_server.service.VisitLogService;
import com.qhg.saas_server.utils.TimeDifferenceUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class VisitLogServiceImpl implements VisitLogService {


    @Autowired
    VisitLogMapper visitLogMapper;

    /**
     * 拜访结果查询 条件查询和封装结果集
     * @param visitLogDto
     * @return
     */
    @Override
    public PageInfo<VisitLogDto> getVisitLogByCondition(VisitLogDto visitLogDto) {

        //设置排序规则
        String orderType = "ASC";
        if(!Objects.isNull(visitLogDto.getSortMode())){
            orderType = visitLogDto.getSortMode()==1? "DESC" :"ASC";
        }
        //设置分页条件查询判断默认为第一页，size为十条数据
        Integer pageNum = visitLogDto.getPageNum();
        Integer pageSize = visitLogDto.getPageSize();
        if(Objects.isNull(pageSize)){
            pageNum = 1;
        }
        if(Objects.isNull(pageSize)){
            pageSize = 10;
        }

        //开启分页插件
        PageHelper.startPage(pageNum,pageSize,"a.startTime  "+orderType);
       List<VisitLogDto> list = visitLogMapper.getVisitLogByCondition(visitLogDto);

       //计算停留时间
       list.stream().forEach((item)->{
           Date startTime = item.getStartTime();
           Date endTime = item.getEndTime();
           if(!Objects.isNull(endTime)){
               Integer time = TimeDifferenceUtil.DateDifference(endTime, startTime);
               item.setStayTime(time);
               log.info("id  "+item.getVisitLogId()+ "   startTime   "+item.getStartTime()+"endTime  "+item.getEndTime()+"stayTime      "+item.getStayTime());
           }
       });
       //封装结构集
        PageInfo<VisitLogDto> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 根据VisitLogIdList查询VisitLogDtoList
     * @param visitLogIdList
     * @return
     */
    @Override
    public List<VisitLogDto> selectVisitLogDtoListByVisitLogIdList(List<Integer> visitLogIdList) {

        List<VisitLogDto> list =  visitLogMapper.selectVisitLogDtoListByVisitLogIdList(visitLogIdList);
        list.stream().forEach((item)->{

            Date startTime = item.getStartTime();
            Date endTime = item.getEndTime();
            if(!Objects.isNull(endTime)){
                Integer time = TimeDifferenceUtil.DateDifference(endTime, startTime);
                item.setStayTime(time);
            }
        });

        return list;
    }

    @Override
    public PageInfo<VisitStatisticsDto> getVisitLogPlusByCondition(VisitStatisticsDto visitStatisticsDto) {

        String sortType = "ASC";
        //确定排序规则
        Integer sortRule = visitStatisticsDto.getSortRule();
        if(!Objects.isNull(sortRule)){
          sortType =    sortRule == 1 ? "DESC" : "ASC";
        }

        //开启分页
        Page<Object> page = PageHelper.startPage(visitStatisticsDto.getPageNum(), visitStatisticsDto.getPageSize(),"startTime  "+sortType);

        //根据条件查询VisitStatisticsDto并且封装为一个list,获取了storeType,storeName,creator,mobilePhone,storeId,visitTimes
        List<VisitStatisticsDto> list = visitLogMapper.getVisitLogPlusByCondition(visitStatisticsDto);

        //获取effectiveTimes并且添加到
        list.forEach((item)->{
            Integer storeId = item.getStoreId();
            Integer effectiveTimes = visitLogMapper.getEffectiveTimes(storeId, visitStatisticsDto);
            item.setEffectiveTimes(effectiveTimes);
        });

        //判断拜访总记录数量
        Integer visitTimes = visitStatisticsDto.getVisitTimes();
        if(!Objects.isNull(visitTimes)){
         list = list.stream().filter((item)->{
               return  item.getVisitTimes()>= visitTimes;
            }).collect(Collectors.toList());
        }
        //判断有效访问次数
        Integer effectiveTimes = visitStatisticsDto.getEffectiveTimes();
        if(!Objects.isNull(effectiveTimes)){
        list  =  list.stream().filter((item)->{
                return item.getEffectiveTimes()>= effectiveTimes;
            }).collect(Collectors.toList());
        }


        //封装分页
        PageInfo<VisitStatisticsDto> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }
}
